<template>
  <div class="edit-container">
    <!-- 顶部栏 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.go(-1)"
    ></van-nav-bar>
    <!-- 图像盒子 -->
    <div class="avater">
      <van-image
        round
        fit="cover"
        width="120px"
        height="120px"
        :src="userInfo.photo"
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- cell列表 -->
    <van-cell-group>
      <van-cell
        title="名称"
        :value="userInfo.name"
        is-link
        @click="showDialogClick"
      />
      <van-cell
        title="性别"
        :value="userInfo.gender ? '女' : '男'"
        is-link
        @click="showPopup = true"
      />
      <van-cell
        title="生日"
        :value="userInfo.birthday"
        is-link
        @click="datePickerClick"
      />
    </van-cell-group>
    <!-- 编辑姓名弹窗 -->
    <van-dialog
      v-model="showDialog"
      title="编辑姓名"
      show-cancel-button
      @confirm="editName"
    >
      <van-field v-model="newName" ref="input" placeholder="请输入姓名" />
    </van-dialog>
    <!-- 编辑性别弹窗 -->
    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar title="修改性别" left-arrow @click-left="showPopup = false">
      </van-nav-bar>
      <van-cell is-link title="男" @click="updateGender(0)" />
      <van-cell is-link title="女" @click="updateGender(1)" />
    </van-popup>
    <!-- 修改生日 -->
    <van-popup position="bottom" v-model="showDatePicker">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showDatePicker = false"
        @confirm="editBirthday"
      />
    </van-popup>
    <!-- 裁图片 -->
    <div class="cropper-box" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        fixed
        autoCropWidth="120"
        autoCropHeight="120"
        autoCrop
      ></vueCropper>
      <van-button @click="showCropper = false" class="cancel" type="primary"
        >取消</van-button
      >
      <van-button @click="editPhotoClick" class="confirm" type="success"
        >确定</van-button
      >
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { editPhoto, editUserInfo } from './user.js'
import moment from 'moment'
import { VueCropper } from 'vue-cropper'
export default {
  name: 'edit',
  data () {
    return {
      showDialog: false, //编辑姓名弹窗
      newName: '', //姓名输入框文本
      showPopup: false,
      showDatePicker: false,
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 12, 1),
      currentDate: new Date(2021, 7, 6),
      img: null,
      showCropper: false
    }
  },
  methods: {
    //点击编辑按钮,弹出编辑姓名弹出
    showDialogClick () {
      //修改输入框
      this.newName = this.userInfo.name
      //弹窗显示
      this.showDialog = true
      //输入框自动聚焦
      this.$nextTick(() => {
        this.$refs.input.focus()
      })
    },
    //编辑姓名确认按钮
    editName () {
      editUserInfo({ name: this.newName }).then(res => {
        // console.log(res);
        this.$toast.success('修改成功')
        this.$store.commit('updateUserInfo', {
          ...this.userInfo, //先取出原先的属性
          name: this.newName //单独覆盖name属性
        })
      })
    },
    //3.修改性别
    updateGender (gender) {
      editUserInfo({ gender }).then(res => {
        //(1)弹窗提示
        this.$toast.success('更新成功!')
        //(2)更新vuex数据
        this.$store.commit('updateUserInfo', {
          ...this.userInfo, //先传入原始数据
          gender: gender //然后使用新的name覆盖原始的name
        })
        //(3)关闭弹窗 popup弹窗默认是点击空白区域关闭。我们点击的是cell需要手动关闭
        this.showPopup = false
      })
    },
    /* 点击生日cell */
    datePickerClick () {
      this.showDatePicker = true
      let date = new Date(this.userInfo.birthday)
      this.currentDate = date
    },
    /* 编辑生日 */
    editBirthday () {
      let birthday = moment(this.currentDate).format('YYYY-MM-DD')
      editUserInfo({ birthday }).then(res => {
        this.$toast.success('修改成功')
        this.$store.commit('updateUserInfo', {
          ...this.userInfo,
          birthday
        })
        this.showDatePicker = false
      })
    },
    /* 选择文件上传 */
    afterRead (file) {
      // 此时可以自行将文件上传至服务器
      // console.log(file)
      this.img = file.content
      //(2)弹出裁切框
      this.showCropper = true
    },
    editPhotoClick() {
      this.$refs.cropper.getCropBlob(data => {
        // do something
        console.log(data)
        let fd = new FormData()
        fd.append('photo', data)
        editPhoto(fd).then(res => {
          this.$toast.success('修改成功')
          this.showCropper=false
          this.$store.commit('updateUserInfo', {
            ...this.userInfo,
            photo: res.data.data.photo
          })
        })
      })
    }
  },
  computed: {
    ...mapState(['userInfo'])
  },
  components: {
    VueCropper //注册组件
  }
}
</script>

<style lang="less">
.edit-container {
  .van-nav-bar {
    background-color: #3196fa;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  }
}
.avater {
  height: 160px;
  display: flex;
  justify-content: center;
  align-items: center;
  .van-uploader {
    position: absolute;
    width: 120px;
    height: 120px;
    opacity: 0;
    .van-uploader__upload {
      width: 120px;
      height: 120px;
      margin: 0px;
    }
  }
}
.cropper-box {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  .cancel {
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .confirm {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
</style>
